﻿using Newtonsoft.Json;
using Ping9719.WpfEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace BaseUi.Db
{
    public static class JoggleModel
    {
        public class Root
        {
            /// <summary>
            /// 测试人员
            /// </summary>
            public string testPerson { get; set; }
            /// <summary>
            /// 测试时间
            /// </summary>
            public string testTime { get; set; }
            /// <summary>
            /// 测试机型
            /// </summary>
            public string productModel { get; set; }
            /// <summary>
            /// 测试结果
            /// </summary>
            public string testResult { get; set; }
        }
        public class Rootget
        {
            /// <summary>
            /// 
            /// </summary>
            public int code { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public int data { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string message { get; set; }
        }
        public static string postmes(Root root)
        {
            string postResponse = "";
            string postReturn = "";

            Logs.Write(root, LogsType.MES);
            try
            {
                using (HttpClient hpc = new HttpClient())
                {
                    //hpc.BaseAddress = new Uri(url);
                    HttpContent httpContent = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(root));
                    httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

                    postResponse = hpc.PostAsync(LocalFile.Config.MESjoggle, httpContent).Result.Content.ReadAsStringAsync().Result;
                    Rootget jo = JsonConvert.DeserializeObject<Rootget>(postResponse);
                    if (jo.code == 0 && jo.message == "ok" && jo.data == 3)
                    {
                        postReturn = "OK";
                    }
                    else
                    {

                        postReturn = "数据上传失败！";
                    }
                    Logs.Write(jo, LogsType.MES);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                postReturn = ex.Message;

                Logs.Write(ex.Message, LogsType.MES);
            }
            return postReturn;
        }
    }
}
